package com.xd.clap.services.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xd.clap.entity.Image;
import com.xd.clap.entity.response.Result;
import com.xd.clap.mapper.ImageMapper;
import com.xd.clap.services.ImageService;
import com.xd.clap.utils.BASE64ToMultipartFile;
import com.xd.clap.utils.SaveImage;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ImageServiceImpl implements ImageService {

    @Resource
    private ImageMapper im;


    @Override
    public Integer insertImage(Image image) {
        return im.insert(image);
    }

    @Override
    public List<Image> selectImageByAid(Integer aid) {
        QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("aid", aid);
        return im.selectList(queryWrapper);
    }

    @Override
    public List<Image> selectImageByRid(Integer rid) {
        QueryWrapper<Image> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("rid", rid);
        return im.selectList(queryWrapper);
    }

    @Override
    public Result uploadImage(MultipartFile image) {
        return SaveImage.saveImage(image);
    }

    @Override
    public Result uploadImageBase64(String imgUrl) {
        MultipartFile file = BASE64ToMultipartFile.base64ToMultipart(imgUrl);
        return SaveImage.saveImage(file);
    }
}
